package com.cybertron.lockdown.spider.handler;


import com.alibaba.fastjson.JSON;
import us.codecraft.webmagic.Spider;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Created by xiezhouyan on 15-1-22.
 */
public class LogHandler extends Handler {

    public LogHandler(BufferedReader in, PrintWriter out, Spider spider, HandleTask task, HandleRequest request) {
        super(in, out, spider, task, request);
    }

    @Override
    public void execute() {
        Scanner scanner = request.getLogScanner();
        while (request.isConnected() && !isStoped() && scanner != null && scanner.hasNextLine()) {
            String line = scanner.nextLine();
            Map<String, Object> response = new HashMap<String, Object>();
            response.put("log", line);
            task.setResponse(response);
            out.println(JSON.toJSONString(task));
            out.flush();
        }
    }
}
